﻿using CommonBase.ResponseBase;
using CommonBase;
using CustomControls.PoptipCtr;
using CustomControls.PoptipCtr.Base;
using LogProjectManager;
using Newtonsoft.Json;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MyAppProjectControl.ViewModels
{
    public class DialogByTimeSelectViewModel : IDalogViewModelBase, ILayDialogWindowAware
    {
        public new event Action<ILayDialogResult> RequestClose;

        private string DeviceId { set; get; }

        private DateTime? _CreatTime;

        public DateTime? CreatTime
        {
            get { return _CreatTime; }
            set { _CreatTime = value; RaisePropertyChanged(); }
        }
        public new bool CanCloseDialog()
        {
            return true;
        }

        #region cmd
        private DelegateCommand _CloseCommand;
        public DelegateCommand CloseCommand =>
            _CloseCommand ?? (_CloseCommand = new DelegateCommand(CloseCommandMethod));


        private DelegateCommand _OKCommand;
        public DelegateCommand OKCommand =>
            _OKCommand ?? (_OKCommand = new DelegateCommand(OKCommandMethod));


        #endregion

        public new void OnDialogClosed()
        {

        }

        public void OnDialogOpened(ILayDialogParameter parameters)
        {
            try
            {
                DeviceId = parameters.GetValue<string>("deviceid");
            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "OnDialogOpened", ex.Message);
            }
        }

        private void CloseCommandMethod()
        {
            RequestClose?.Invoke(null);

        }

        private async void OKCommandMethod()
        {
            CommonStaticModel.LastCmdType = "上传日志命令";
            string apiUrl = $"http://192.168.0.41:8081/api/ToSendCmd?_deviceid={DeviceId}&_cmd={Convert.ToInt32(CmdSendType.uploadLog)}";
            using (HttpClient client = new HttpClient())
            {
                string postData = "";
                HttpContent httpContent = new StringContent(postData);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                httpContent.Headers.ContentType.CharSet = "utf-8";

                HttpResponseMessage response = await client.PostAsync(apiUrl, httpContent);

                if (response.IsSuccessStatusCode)
                {
                    var read = response.Content.ReadAsStringAsync();
                    read.Wait();
                    ResponseBody json = JsonConvert.DeserializeObject<ResponseBody>(read.Result);
                    if (json.Code == ErrorCode.OK)
                    {
                        MessageControl.Warning("操作成功!", "RootMessageTooken");
                    }
                    else
                    {
                        MessageControl.Warning("操作失败!", "RootMessageTooken");
                        LogManager.WriteLog(LogTypeEnum.Exception, "UpdateCommandMethod", json.Data.ToString());
                    }
                }
                else
                {
                    MessageControl.Warning("操作失败!", "RootMessageTooken");
                }
            }
        }

    }
}
